#include<bits/stdc++.h>
using namespace std;
const int N=205;

int n,m;
struct Node{
    int s,t,w;
    Node(){}
    Node(int s,int t,int w){
        this->s=s;
        this->t=t;
        this->w=w;
    }
    bool operator<(const Node& node) const{
        return this->w<node.w;
    }
    bool operator==(const Node& node) const{
        return this->s==node.s&&this->t==node.t&&this->w==node.w;
    }
};
multiset<Node> nodes;
int fa[N];
int find(int x){
    if(x != fa[x]) fa[x] = find(fa[x]);
    return fa[x];
}
void merge(int a,int b){
    fa[find(a)]=find(b);
}

int main(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        int s,t,w0;
        scanf("%d%d%d",&s,&t,&w0);
        nodes.insert(Node(s,t,w0));
        for(int j=1;j<=n;j++){
	        fa[j]=j;
	    }
        int es=0;
        int ans=0;
        for(auto e:nodes){
            int s=e.s,t=e.t,w=e.w;
            //printf("edges show: %d  %d  %d\n",s,t,w);
            if(find(s)==find(t)) continue;
            ans+=w;
            es++;
            merge(s,t);
            //printf("edges choose: %d  %d  %d\n",s,t,w);
            if(es==n-1) break;
        }
        if(es==n-1){
            printf("%d\n",ans);
        }
        else{
            printf("-1\n");
        }
        // printf("fath:");
        // for(int j=1;j<=n;j++){
        //     printf("%d ",fa[j]);
        // }
        // printf("\n");
    }
    return 0;
}
